package com.imedway.ihe.dtz.xds.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EbxmlInfoUtils {

	public static void main(String[] args) {

	}
	
	public static boolean saveEbxmlInfo(EbXMLSubmitObjectsRequest30 ebXMLBody) {
		boolean flag = true;
		try {
			
			List<EbXMLExtrinsicObject> ebXMLExtrinsicObjectList =	ebXMLBody.getExtrinsicObjects();
			if(ebXMLExtrinsicObjectList!=null) {
				saveEbXMLExtrinsicObjectList(ebXMLExtrinsicObjectList);
			}
			
			List<EbXMLRegistryPackage> ebXMLRegistryPackageList= ebXMLBody.getRegistryPackages();
			if(ebXMLRegistryPackageList!=null) {
				saveEbXMLRegistryPackageList(ebXMLRegistryPackageList);
			}
			
			List<EbXMLAssociation> ebXMLAssociationList = ebXMLBody.getAssociations();
			if(ebXMLAssociationList!=null) {
				saveEbXMLAssociationList(ebXMLAssociationList);
			}
			
			List<EbXMLClassification> ebXMLClassificationList = ebXMLBody.getClassifications();
			if(ebXMLClassificationList!=null) {
				saveEbXMLClassificationList(ebXMLClassificationList);
			}
			
			List<EbXMLSlot> ebXMLSlotList = ebXMLBody.getSlots();
			if(ebXMLSlotList!=null) {
				saveEbXMLSlotList(ebXMLSlotList);
			}
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}

	public static boolean saveEbXMLExtrinsicObjectList(List<EbXMLExtrinsicObject> ebXMLExtrinsicObjectList) {
		boolean flag = true;
		try {
			Connection conn = MysqlDbUtils.getConn();
			log.info(JSONUtil.toJsonStr(ebXMLExtrinsicObjectList));
			String sql = "insert into extrinsicobject ("
					+ "id,home,lid,objectType,status,"
					+ "versionName,comment_,isOpaque,mimeType,contentVersionName,"
					+ "contentVersionComment"
					+ ") "
					+ "value"
					+ "("
					+ "?,?,?,?,?,"
					+ "?,?,?,?,?,"
					+ "?"
					+ ")";
			PreparedStatement stmt = conn.prepareStatement(sql);
			for(EbXMLExtrinsicObject iheXdsInfo : ebXMLExtrinsicObjectList) {
				stmt.setString(1, iheXdsInfo.getId());
				stmt.setString(2, iheXdsInfo.getHome());
				stmt.setString(3, iheXdsInfo.getLid());
				stmt.setString(4, iheXdsInfo.getObjectType());
				stmt.setString(5, iheXdsInfo.getStatus()!=null?iheXdsInfo.getStatus().toString():null);
				stmt.setString(6, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getVersionName():null);
				stmt.setString(7, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getComment():null);
				stmt.setString(8, iheXdsInfo.getStatus()!=null?iheXdsInfo.getStatus().getQueryOpcode():null);
				stmt.setString(9, iheXdsInfo.getMimeType());
				stmt.setString(10, null);
				stmt.setString(11, null);
				stmt.addBatch();
			}			
			int[] batchResults = stmt.executeBatch();
			for(int i:batchResults) {
				log.info(String.valueOf(i));
			}
			if(stmt!=null) {
				stmt.close();
			}
			if(conn!=null) {
				conn.close();
			}

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}

	public static boolean saveEbXMLRegistryPackageList(List<EbXMLRegistryPackage> ebXMLRegistryPackageList) {
		boolean flag = true;
		try {
			Connection conn = MysqlDbUtils.getConn();
			log.info(JSONUtil.toJsonStr(ebXMLRegistryPackageList));
			String sql = "insert into registrypackage ("
					+ "id,home,lid,objectType,status,"
					+ "versionName,comment_"
					+ ") "
					+ "value"
					+ "("
					+ "?,?,?,?,?,"
					+ "?,?"
					+ ")";
			PreparedStatement stmt = conn.prepareStatement(sql);
			for(EbXMLRegistryPackage iheXdsInfo : ebXMLRegistryPackageList) {
				stmt.setString(1, iheXdsInfo.getId());
				stmt.setString(2, iheXdsInfo.getHome());
				stmt.setString(3, iheXdsInfo.getLid());
				stmt.setString(4, iheXdsInfo.getObjectType());
				stmt.setString(5, iheXdsInfo.getStatus()!=null?iheXdsInfo.getStatus().toString():null);
				stmt.setString(6, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getVersionName():null);
				stmt.setString(7, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getComment():null);
				stmt.addBatch();
			}			
			int[] batchResults = stmt.executeBatch();
			for(int i:batchResults) {
				log.info(String.valueOf(i));
			}
			if(stmt!=null) {
				stmt.close();
			}
			if(conn!=null) {
				conn.close();
			}

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	public static boolean saveEbXMLAssociationList(List<EbXMLAssociation> ebXMLAssociationList) {
		boolean flag = true;
		try {
			Connection conn = MysqlDbUtils.getConn();
			log.info(JSONUtil.toJsonStr(ebXMLAssociationList));
			String sql = "insert into association ("
					+ "id,home,lid,objectType,status,"
					+ "versionName,comment_,associationType,sourceObject,targetObject"
					+ ") "
					+ "value"
					+ "("
					+ "?,?,?,?,?,"
					+ "?,?,?,?,?"
					+ ")";
			PreparedStatement stmt = conn.prepareStatement(sql);
			for(EbXMLAssociation iheXdsInfo : ebXMLAssociationList) {
				stmt.setString(1, iheXdsInfo.getId());
				stmt.setString(2, iheXdsInfo.getHome());
				stmt.setString(3, iheXdsInfo.getLid());
				stmt.setString(4, iheXdsInfo.getObjectType());
				stmt.setString(5, iheXdsInfo.getStatus()!=null?iheXdsInfo.getStatus().toString():null);
				stmt.setString(6, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getVersionName():null);
				stmt.setString(7, iheXdsInfo.getVersionInfo()!=null?iheXdsInfo.getVersionInfo().getComment():null);
				stmt.setString(8, iheXdsInfo.getAssociationType().getOpcode30());
				stmt.setString(9, iheXdsInfo.getSource());
				stmt.setString(10, iheXdsInfo.getTarget());
				stmt.addBatch();
			}			
			int[] batchResults = stmt.executeBatch();
			for(int i:batchResults) {
				log.info(String.valueOf(i));
			}
			if(stmt!=null) {
				stmt.close();
			}
			if(conn!=null) {
				conn.close();
			}

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	@SuppressWarnings("static-access")
	public static boolean saveEbXMLClassificationList(List<EbXMLClassification> ebXMLClassificationList) {
		boolean flag = true;
		try {
			Connection conn = MysqlDbUtils.getConn();
			log.info(JSONUtil.toJsonStr(ebXMLClassificationList));
			String sql = "insert into classification ("
					+ "id,home,lid,objectType,status,"
					+ "versionName,comment_,classificationNode,classificationScheme,classifiedObject,"
					+ "nodeRepresentation"
					+ ") "
					+ "value"
					+ "("
					+ "?,?,?,?,?,"
					+ "?,?,?,?,?,"
					+ "?"
					+ ")";
			PreparedStatement stmt = conn.prepareStatement(sql);
			for(EbXMLClassification iheXdsInfo : ebXMLClassificationList) {
				stmt.setString(1, UUID.fastUUID().toString());
				stmt.setString(2, null);
				stmt.setString(3, null);
				stmt.setString(4, iheXdsInfo.CLASSIFICATION_OBJECT_TYPE);
				stmt.setString(5, "");
				stmt.setString(6, null);
				stmt.setString(7, null);
				stmt.setString(8, iheXdsInfo.getClassificationNode());
				stmt.setString(9, iheXdsInfo.getClassificationScheme());
				stmt.setString(9, iheXdsInfo.getClassifiedObject());
				stmt.setString(11, iheXdsInfo.getNodeRepresentation());
				stmt.addBatch();
			}			
			int[] batchResults = stmt.executeBatch();
			for(int i:batchResults) {
				log.info(String.valueOf(i));
			}
			if(stmt!=null) {
				stmt.close();
			}
			if(conn!=null) {
				conn.close();
			}

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	public static boolean saveEbXMLSlotList(List<EbXMLSlot> ebXMLSlotList) {
		boolean flag = true;
		try {
			Connection conn = MysqlDbUtils.getConn();
			log.info(JSONUtil.toJsonStr(ebXMLSlotList));
			String sql = "insert into slot ("
					+ "sequenceId,name_,slotType,value,parent"
					+ ") "
					+ "value"
					+ "("
					+ "?,?,?,?,?"
					+ ")";
			PreparedStatement stmt = conn.prepareStatement(sql);
			for(EbXMLSlot iheXdsInfo : ebXMLSlotList) {
				stmt.setString(1, null);
				stmt.setString(2, iheXdsInfo.getName());
				stmt.setString(3, null);
				stmt.setString(4, JSONUtil.toJsonStr(iheXdsInfo.getValueList()));
				stmt.setString(5, null);
				stmt.addBatch();
			}			
			int[] batchResults = stmt.executeBatch();
			for(int i:batchResults) {
				log.info(String.valueOf(i));
			}
			if(stmt!=null) {
				stmt.close();
			}
			if(conn!=null) {
				conn.close();
			}

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
}
